import LIM from "./lim-js-sdk.js"
import store from '@/store/index.js'
import conversational_api from '@/common/api/conversational.js'

//监听收到消息
LIM.on('MESSAGE_RECEIVED', (res) => {
	console.log(res);
	let data = res.data;
	//会话id
	let conversational = data.conversational;
	let list = uni.$u.deepClone(store.getters.conversationalList);
	let index = list.findIndex((item) => {
		return conversational.conversationalId === item.conversationalId;
	});
	if (index > -1) {
		//存在，删除一位
		list.splice(index, 1);
	}
	list.unshift(conversational);
	uni.$u.vuex('vuex_conversational_list', list);
	conversational_api.listUnreadCount();
});

const $im = {
	login: function(url, u, p, d) {
		LIM.login(url, u, p, d);
	},

	logout: function() {
		return LIM.logout();
	}
}

module.exports = (vm) => {
	uni.$im = $im;
}
